﻿Imports System.IO

Public Class frmCourtClerk

    Dim _PhotoPath As String = ""
    Public _CaseID As Long
    Public _CourtClerkID As Long

    Private Sub frmCourtClerk_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Reset()
        If _CourtClerkID <> 0 Then
            getTextbox()
        End If
    End Sub

    Protected Sub getTextbox()
        Dim oCourtClerk As New cCourtClerk
        Dim dt As New DataTable
        dt = oCourtClerk.select_CourtClerk_byID(_CourtClerkID)
        Try
            txtName.Text = dt.Rows(0)("CourtClerkName")
            cboGender.Text = dt.Rows(0)("Gender")
            dtpDOB.Value = dt.Rows(0)("DOB")
            txtAddress.Text = dt.Rows(0)("Address")
            txtTel.Text = dt.Rows(0)("Tel")
            txtEmail.Text = dt.Rows(0)("Email")
            'For Image
            If dt.Rows(0)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(0)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.LogoCompany
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub EnableControl()
        txtName.Enabled = False
        cboGender.Enabled = False
        dtpDOB.Enabled = False
        txtAddress.Enabled = False
        txtTel.Enabled = False
        txtEmail.Enabled = False
        picPhoto.Enabled = False
        btnChoose.Enabled = False
        btnRemove.Enabled = False
        btnSave.Enabled = False
        btnClean.Enabled = False
    End Sub

    Protected Sub reset()
        txtName.Clear()
        If cboGender.Items.Count > 0 Then
            cboGender.SelectedIndex = 0
        Else
            cboGender.SelectedIndex = -1
        End If
        dtpDOB.Value = #1/1/1980#
        txtAddress.Clear()
        txtTel.Clear()
        txtEmail.Clear()
        picPhoto.Image = My.Resources.Photo
        _PhotoPath = ""
        txtName.Focus()
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            picPhoto.ImageLocation = _PhotoPath
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        picPhoto.Image = My.Resources.Photo
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtName.Text) And oc.checknull(cboGender.Text) And oc.checknull(dtpDOB.Value) And oc.checknull(txtAddress.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtEmail.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim oCourtClerk As New cCourtClerk
        If checknull() Then
            If oCourtClerk.insert_CourtClerk(txtName.Text.Trim, cboGender.Text, dtpDOB.Value, txtAddress.Text, txtTel.Text.Trim, txtEmail.Text.Trim, getPhoto(), _CaseID, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getCourtClerk()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If _PhotoPath <> "" Then
            pic = File.ReadAllBytes(_PhotoPath)
        End If
        Return pic
    End Function

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub
End Class